﻿' ----------------------------------------------------------------------
'
' DirectoryEntity.vb
'
'   Dbfs.Domain
'
'   This class contains the implementation of a single folder.
'
' ----------------------------------------------------------------------
' version:  1.0.0.1
' today:    09/07/09  12:45:00
' ident:    "@(#)/Dbfs/Domain/DirectoryEntity.vb 1.0 0.1 09/07/09 0 rjs"

Imports System.IO
Imports System.Text.RegularExpressions


Namespace Dbfs

    ''' <summary>
    ''' (DBFS) Exposes instance methods for creating, moving, and enumerating through 
    ''' directories and subdirectories. This class cannot be inherited.
    ''' </summary>
    ''' <remarks>Use the DirectoryInfo class for typical operations such as 
    ''' copying, moving, renaming, creating, and deleting directories.</remarks>
    <SerializableAttribute()> _
    Public Class DirectoryEntity
        Inherits FileSystemEntity

        ''' <summary>
        ''' An array of child entities.
        ''' </summary>
        ''' <remarks></remarks>
        Private _children As EntityCollection

        ''' <summary>
        ''' A flag indicating if this entity has child entities.
        ''' </summary>
        ''' <remarks></remarks>
        Private _hasChildren As Boolean


#Region " Constructors "
        Public Sub New()
            MyBase.New()
        End Sub

        ''' <summary>
        ''' Initializes a new instance of the <see cref="DirectoryEntity"/> class.
        ''' </summary>
        ''' <param name="path">A string specifying the path on which to create the <b>DirectoryEntity</b>.</param>
        ''' <remarks>If the path is fully qualified with a UNC name, a folder is created that
        ''' can be persisted in that filesystem.  If the path does not contain a UNC name,
        ''' a folder is created within the virtual space for the current user.</remarks>
        Public Sub New(ByVal path As String)
            MyBase.New(path)
        End Sub
#End Region ' Constructors

#Region " Properties "
        ''' <summary>
        ''' Gets the array of child elements of this object.
        ''' </summary>
        ''' <value></value>
        ''' <returns>The array of child FileSystemInfo objects.</returns>
        ''' <remarks>This property is read-only.</remarks>
        Public Overridable Property Children() As EntityCollection
            Get
                If _children Is Nothing Then
                    _children = New EntityCollection
                End If
                Return _children
            End Get
            Set(ByVal value As EntityCollection)
                _children = value
            End Set
        End Property

        ''' <summary>
        ''' Indicates whether the hierarchical data node that the IHierarchyData 
        ''' object represents has any child nodes.
        ''' </summary>
        ''' <value><b>true</b> if the current node has child nodes; otherwise, <b>false</b>.</value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overridable ReadOnly Property HasChildren() As Boolean
            Get
                Return _hasChildren
            End Get
        End Property
#End Region ' Properties

#Region " Methods "
        Public Function Copy() As FileSystemEntity

            ' local variables
            Dim entity As DirectoryEntity
            Dim item As FileSystemItem

            ' get a basic copy (a shallow copy)
            entity = Me.MemberwiseClone

            ' replace the history set)
            entity.History = New FileSystemItemSet

            ' is it the job of the entity to copy children to just make space?
            entity._children = _children.Clone

            item = FileSystemItem.Clone
            entity.History.Add(item)

            Return entity

        End Function

        Public Overridable Function GetFileSystemInfos() As FileSystemInfo()
            Dim fsinfo() As FileSystemInfo = {}
            Array.Resize(fsinfo, Children.Count)
            For i As Int16 = 0 To Children.Count - 1
                If Children(i).Attributes And FileAttributes.Directory Then
                    fsinfo(i) = New DirectoryInfo(Children(i).FullName)
                Else
                    fsinfo(i) = New FileInfo(Children(i).FullName)
                End If
            Next
            Return fsinfo
        End Function
#End Region ' Methods

    End Class

End Namespace
